﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DevExpress.Web.Mvc;
using EnterpriseFund.Common;
using Newtonsoft.Json;
using DevExpress.Web.ASPxGridView;
using System.Web.UI;
using EnterpriseFund.BLL;

namespace EnterpriseFund.Controllers
{
    public class QueryAllowanceStatusController : Controller
    {
        //
        // GET: /QueryAllowanceStatus/

        public ActionResult Index()
        {
            EnterpriseFund.BLL.QueryAllowanceStatus QueryAllowanceInfobll = new EnterpriseFund.BLL.QueryAllowanceStatus();
            var zsxx = QueryAllowanceInfobll.GetModelList("");
            //BLL.FinanceProp f = new BLL.FinanceProp();
            //BLL.EnterpriseProp p = new BLL.EnterpriseProp();
            //var vf = f.GetAllList();
            //var vp = p.GetAllList();
            //ViewData["FinancePropList"] = vf;
            //ViewData["EnterprisePropList"] = vp;
          
            return View("Index", zsxx);
            // return View();
        }
        public ActionResult QueryAllowanceStatusListPartial()
        {
          //  GetWhereString();
            var vsbzsxx = GetModelVSBZSXX();
            //foreach (Model.QueryAllowance x in vsbzsxx)
            //{
            //    x.FAuditAmount = decimal.Parse(x.FAuditAmount.ToString()) / 10000;
            //}
            return View(vsbzsxx);
        }
        #region 导出EXCEL
        public ActionResult ExportToXls()
        {
            var table = GetModelVSBZSXX();
            return GridViewExtension.ExportToXls(ExportGridViewSettings, table, HttpUtility.UrlEncode("补贴状态查询导出.xls"), true);
        }

        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {

            get
            {

                if (exportGridViewSettings == null)

                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }
        private string IsNull(string s1, string s2)
        {
            return string.IsNullOrEmpty(s1) ? s2 : s1;
        }
        //public string ClearViewBag()
        //{
        //    try
        //    {
        //        ViewBag=null;
        //        return "OK";
        //    }
        //    catch (System.Exception ex)
        //    {
        //        return "error";
        //    }
        //}
        public string GetWhereString()
        {
            //绑定值

            //var FTaxClassAllowanceName = IsNull(ComboBoxExtension.GetValue<string>("FTaxClassAllowanceName"), Request["FTaxClassAllowanceName"]);
            //  var nKHYH = Request["NKHYH"];
            var NSRMC = Request["NSRMC"];
            var FFinance = IsNull(ComboBoxExtension.GetValue<string>("nodename1"), Request["FFinance"]);
            var NSRBM = Request["NSRBM"];
            var NSRSBH = Request["NSRSBH"];
            var FNewEnterprise = IsNull(ComboBoxExtension.GetValue<string>("nodestatus1"), Request["FNewEnterprise"]);
            //var FIssueID = Request["FIssueID"];
            //var ISPAY = Request["ISPAY"];
            //var FYear = Request["FYear"];
            //var BTJEA = Request["BTJEA"];
            //var BTJEB = Request["BTJEB"];
            //var FApplyDate = Request["FApplyDate"];
            //var LastPayDate = Request["LastPayDate"];
            //var ISBASE = Request["ISBASE"];
            //var FFileNo = Request["FFileNo"];
            //var SKSJA = Request["SKSJA"];
            //var SKSJB = Request["SKSJB"];
            ViewBag.NSRMC = NSRMC;
            ViewBag.nodename = FFinance;
            ViewBag.NSRBM = NSRBM;
            ViewBag.NSRSBH = NSRSBH;
            ViewBag.nodestatus = FNewEnterprise;
            //ViewBag.FIssueID = FIssueID;
            //ViewBag.ISPAY = ISPAY;
            //ViewBag.FYear = FYear;
            //ViewBag.BTJEA = BTJEA;
            //ViewBag.BTJEB = BTJEB;
            //ViewBag.FApplyDate = FApplyDate;
            //ViewBag.LastPayDate = LastPayDate;
            //ViewBag.ISBASE = ISBASE;
            //ViewBag.FFileNo = FFileNo;
            //ViewBag.SKSJA = SKSJA;
            //ViewBag.SKSJB = SKSJB;
            //ViewBag.FAmount1 = FAmount1;
            //ViewBag.FAmount2 = FAmount2;
            //ViewBag.FFileNo = FFileNo;
            //    //   ViewBag.FNote = FNote;
            //    //   ViewBag.FPayDateBegin = FPayDateBegin;
            //    //   ViewBag.FPayDateEnd = FPayDateEnd;
            //     //  ViewBag.nsrbm = nsrbm;
            //       ViewBag.nsrmc = nsrmc;
            //       ViewBag.nKHYH = nKHYH;
            //       ViewBag.FFinance = FFinance;
            var filter = "1=1";
            //if (!string.IsNullOrEmpty(ISPAY) && ISPAY == "true")
            //    filter += string.Format("and NeedAmount =0 ");

            //if (!string.IsNullOrEmpty(ISBASE) && ISBASE == "true")
             //   filter += string.Format("and FBaseAmount >0 ");
            //if (!string.IsNullOrEmpty(SFBF) && SFBF == "false")
            //    filter += string.Format("and FAuditAmount > FAmount ");
            //if (!string.IsNullOrEmpty(nsrbm))
            //    filter += string.Format("and NSRBM like '%{0}%'", nsrbm);
            if (!string.IsNullOrEmpty(NSRMC))
                filter += string.Format("and NSRMC like '%{0}%' ", NSRMC);
            if (!string.IsNullOrEmpty(NSRBM))
                filter += string.Format("and NSRBM like '%{0}%' ", NSRBM);
            if (!string.IsNullOrEmpty(NSRSBH))
                filter += string.Format("and NSRSBH like '%{0}%' ", NSRSBH);
            //if (!string.IsNullOrEmpty(FIssueID))
            //    filter += string.Format("and FIssueID like '%{0}%' ", FIssueID);
            //if (!string.IsNullOrEmpty(FYear))
            //    filter += string.Format("and FYear = '{0}' ", FYear);
            if (!string.IsNullOrEmpty(FFinance))
                filter += string.Format("and nodename = '{0}' ", FFinance);
            if (!string.IsNullOrEmpty(FNewEnterprise))
                filter += string.Format("and nodestatus= '{0}' ", FNewEnterprise);

            //if (!string.IsNullOrEmpty(FFileNo))
            //    filter += string.Format("and FFileNo like '%{0}%'", FFileNo);
            //if (!string.IsNullOrEmpty(BTJEA) & !string.IsNullOrEmpty(BTJEB))
            //{
            //    filter += string.Format(" and FAuditAmount > '{0}'and FAuditAmount < '{1}' ", BTJEA, BTJEB);
            //}
            //if (!string.IsNullOrEmpty(SKSJA) & !string.IsNullOrEmpty(SKSJB))
            //{
            //    filter += string.Format(" and se > '{0}'and se < '{1}' ", SKSJA, SKSJB);
           // }
            //if (!string.IsNullOrEmpty(FAmount))
            //{
            //    filter += string.Format(" and FAmount = '{0}' ", FAmount);
            //}
            //if (FAmount1 != "" && FAmount2 != "")
            //{
            //    filter += " and FAmount >=" + FAmount1 + " and FAmount<=" + FAmount2 + " ";
            //}
            //else if (FAmount1 != "")
            //{
            //    filter += " and FAmount >=" + FAmount1;
            //}
            //else if (FAmount2 != "")
            //{
            //    filter += " and FAmount <=" + FAmount2;
            //}
            //if (!string.IsNullOrEmpty(FFileNo))
            //{
            //    filter += string.Format(" and FFileNo = '{0}' ", FFileNo);
            //}

            //if (!string.IsNullOrEmpty(FApplyDate) || !string.IsNullOrEmpty(LastPayDate))
            //{
            //    var begin = string.IsNullOrEmpty(FApplyDate) ? DateTime.MinValue.ToString() : FApplyDate;
            //    var end = string.IsNullOrEmpty(LastPayDate) ? DateTime.MaxValue.ToString() : LastPayDate;
            //    filter += string.Format(" AND LastPayDate <= '{0}'", end);
            //    if (!string.IsNullOrEmpty(FApplyDate))
            //        filter += string.Format(" and FApplyDate >= '{0}'", begin);
            //}

            return filter;

        }
        public List<Model.QueryAllowanceStatus> GetModelVSBZSXX()
        {
            var strWhere = GetWhereString();
            EnterpriseFund.BLL.QueryAllowanceStatus QueryAllowanceInfobll = new EnterpriseFund.BLL.QueryAllowanceStatus();
            var zsxx = QueryAllowanceInfobll.GetModelList(strWhere);
            return zsxx;
        }
        public static GridViewSettings CreateExportGridViewSettings()
        {
            var setting = new GridViewSettings();
            setting.Name = "gvEditing";
            setting.KeyFieldName = "FApplicationFormID";
            setting.CallbackRouteValues = new { Controller = "QueryAllowanceStatus", Action = "QueryAllowanceStatusListPartial" };
            setting.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            setting.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;
            setting.Columns.Add("NSRBM", "纳税人编码");
            setting.Columns.Add("NSRSBH", "纳税人识别号");
            setting.Columns.Add("NSRMC", "纳税人名称");
            setting.Columns.Add("NeedAmount", "可补贴金额");
            setting.Columns.Add("nodename", "节点名称");
            setting.Columns.Add("nodestatus", "处理状态");
          //  setting.Columns.Add("LastPayDate", "补贴日期至");
            //setting.Columns.Add("FAuditAmount", "核定金额");  

            //setting.Columns.Add("FAmount", "已补助金额");
            //setting.Columns.Add("NeedAmount", "还需补助金额");
            //    setting.Columns.Add("FNote", "备注");
            setting.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            setting.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;

            setting.SettingsBehavior.AllowFocusedRow = true;
            setting.SettingsLoadingPanel.Text = "数据读取中...";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Count, "NSRSBH").DisplayFormat = "总计:";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAuditAmount").DisplayFormat = "￥{0}";
            setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAuditAmount").DisplayFormat = "合计:￥{0}";
            //setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAmount").DisplayFormat = "￥{0}";
            //setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAmount").DisplayFormat = "合计:￥{0}";

            //setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "NeedAmount").DisplayFormat = "￥{0}";
            //setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "NeedAmount").DisplayFormat = "合计:￥{0}";
            setting.Settings.ShowGroupPanel = true;
            setting.Settings.ShowFooter = true;
            setting.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            setting.ClientSideEvents.EndCallback = "function(){}";

            setting.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                    }
                }
            };

            return setting;
        }

        #endregion
    }
}


